package com.bigegg.employee.api;

import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bigegg.employee.util.CommonUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.bigegg.employee.domain.College;
import com.bigegg.employee.service.ICollegeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学院Controller
 * 
 * @author 刘博
 * @date 2022-08-11
 */
@RestController
@RequestMapping("/employee/college")
public class CollegeController extends BaseController
{
    @Autowired
    private ICollegeService collegeService;

    /**
     * 查询学院列表
     */
    @PreAuthorize("@ss.hasPermi('employee:college:list')")
    @GetMapping("/list")
    public TableDataInfo list(College college)
    {
        startPage();
        List<College> list = collegeService.selectCollegeList(college);
        return getDataTable(list);
    }

    /**
     * 导出学院列表
     */
    @PreAuthorize("@ss.hasPermi('employee:college:export')")
    @Log(title = "学院", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, College college)
    {
        List<College> list = collegeService.selectCollegeList(college);
        ExcelUtil<College> util = new ExcelUtil<College>(College.class);
        util.exportExcel(response, list, "学院数据");
    }

    /**
     * 获取学院详细信息
     */
    @PreAuthorize("@ss.hasPermi('employee:college:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(collegeService.selectCollegeById(id));
    }

    /**
     * 新增学院
     */
    @PreAuthorize("@ss.hasPermi('employee:college:add')")
    @Log(title = "学院", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody College college)
    {
        return toAjax(collegeService.insertCollege(college));
    }

    /**
     * 修改学院
     */
    @PreAuthorize("@ss.hasPermi('employee:college:edit')")
    @Log(title = "学院", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody College college)
    {
        return toAjax(collegeService.updateCollege(college));
    }

    /**
     * 删除学院
     */
    @PreAuthorize("@ss.hasPermi('employee:college:remove')")
    @Log(title = "学院", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(collegeService.deleteCollegeByIds(ids));
    }

    /**
     * 上传excel
     */
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file)
    {

        this.collegeService.importData(file);
        return AjaxResult.success();
    }

    /**
     * 下载模板
     */
    @PostMapping("/downloadTpl")
    public void downloadTpl(HttpServletRequest request, HttpServletResponse response)
    {

        // 模板的路径
        String tplPath = "tpl/学院导入模板.xlsx";

        CommonUtil.downloadResource(tplPath,response);

    }
}
